package org.jeecg.modules.service.patient.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.bean.entity.patient.Brjbxx;
import org.jeecg.modules.bean.entity.patient.Patient;
import org.jeecg.modules.mapper.patient.PatientMapper;
import org.jeecg.modules.service.patient.IPatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: patient
 * @Author: jeecg-boot
 * @Date:   2023-11-09
 * @Version: V1.0
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements IPatientService {

    @Autowired
    private BrjbxxServiceImpl brjbxxServiceImpl;

    @Override
    public List<Patient> getUserPage(Patient patient, Integer pageNo, Integer pageSize, HttpServletRequest req) {
        LambdaQueryWrapper<Patient> queryWrapper = new LambdaQueryWrapper<>();
        String name = patient.getName();
        Page<Patient> page = new Page<>(pageNo, pageSize);
        List<Patient> list;
        //筛选certificateno 不为空串的记录
        queryWrapper.and(wrapper -> wrapper.ne(Patient::getCertificateno,""));
        if (!oConvertUtils.isEmpty(name)){
            queryWrapper.and(wrapper ->wrapper
                            .like(Patient::getName, name)
                            .or()
                            .like(Patient::getCertificateno, name));
            list = this.list(queryWrapper);
            List<Brjbxx> brjbxxList= brjbxxServiceImpl.queryMzxt(name);
            List<Patient> finalList = new ArrayList<>();
            finalList.addAll(list);
            brjbxxList.forEach(b ->{
                Patient p = new Patient();
                p.setName(b.getCbrxm());
                p.setSexno(b.getCbrxb());
                p.setBirthday(b.getDcsny());
                p.setMzblh(b.getMzblh());
                p.setCertificateno(b.getCsfzh());
                p.setTelephone(b.getClxdh());
                p.setType(b.getType());
                finalList.add(p);
            });
            return finalList;
        } else {
            IPage<Patient> pageList = this.page(page, queryWrapper);
            list = pageList.getRecords();
            return list;
        }
    }
}
